package com.mao;

public class ArrayDemo5 {
    public static void main(String[] args) {
        // 目标：二维数组的认识
        printArray();
    }
    public static void printArray() {
        //初始化二维数组存储学生姓名，考虑到座位不规则，直接定义每排的座位
        //静态初始化
        String[][] classroom = {
                {"张三","李四","王五","赵六"},//第一排
                {"孙七","周八"},//第二排
                {"王十一","赵十二","孙十三","周十四"},//第三排
                {"吴十五","郑十六","孙十七"}//第四排
        };

        //原始状态展示
        print(classroom);
        System.out.println("=====================");

        //将classroom数组中的数据打乱
        scramble(classroom, 100);
        //动态初始化
        String[][] classroom2 = new String[4][];
        classroom2[0] = new String[]{"张三","李四","王五","赵六"};
        classroom2[1] = new String[]{"孙七","周八"};
        classroom2[2] = new String[]{"王十一","赵十二","孙十三","周十四"};
        classroom2[3] = new String[]{"吴十五","郑十六","孙十七"};
        //遍历二维数组
        //乱序后展示
        print(classroom);
    }
    public static void print(String[][] classroom){
        //遍历二维数组
        for (int i = 0; i < classroom.length; i++) {
            for (int j = 0; j < classroom[i].length; j++) {
                System.out.print(classroom[i][j]+"\t");
            }
            System.out.println();
        }
    }
    public static void scramble(String[][] classroom, int cnt){
        //将二维数组中的数据打乱
        while (cnt-- != 0) {
            for (int i = 0; i < classroom.length; i++) {
                int row = (int) (Math.random() * classroom.length);
                if (i != row) {
                    String[] temp = classroom[i];
                    classroom[i] = classroom[row];
                    classroom[row] = temp;
                }
                for (int j = 0; j < classroom[i].length; j++) {
                    int random = (int) (Math.random() * classroom[i].length);
                    String temp = classroom[i][j];
                    classroom[i][j] = classroom[i][random];
                    classroom[i][random] = temp;
                }
            }
        }
    }
}
